﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

namespace Hengbei.Web.Admin
{
    public partial class PictureManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void dvPicure_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            gvPicures.DataBind();
        }

        protected void dvPicture_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            if (dvPicture.CurrentMode == DetailsViewMode.Insert)
            {
                FileUpload fupPicture = (FileUpload)dvPicture.FindControl("fuPicture");
                string fileName = ((FileUpload)dvPicture.FindControl("fuPicture")).FileName;
                string extendName = System.IO.Path.GetExtension(fileName).ToLower();
                DropDownList ddlCategory = (DropDownList)dvPicture.FindControl("ddlCategories");
               
                string time = DateTime.Now.ToString("yyyyMMddhhMMss");
                fileName = time + fileName;
                int userID = Convert.ToInt32("1");
                if (userID == 0)
                {
                    Response.Write("<script language='javascript'>alert('请先登入');</script>");
                }
                else
                {
                    string path;
                    string sypath = "thumb";
                    string imagesFolder = ConfigurationManager.AppSettings["ImagesFolderName"].ToString();
                    string virtualpath = "../" + imagesFolder + "/" + ddlCategory.SelectedValue + "/" + DateTime.Now.Year;
                    if (extendName == ".jpg" || extendName == ".gif" || extendName == ".bmp" || extendName == ".png")
                    {
                        path = Server.MapPath(virtualpath);
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                        fupPicture.SaveAs(path + "/" + fileName);
                        System.Drawing.Image image, thumbImage;
                        System.Drawing.Image.GetThumbnailImageAbort callb = null;

                        string imagePath = path + "/" + fileName;
                        string thumbImagePath = path + "/" + sypath + fileName;
                        image = System.Drawing.Image.FromFile(imagePath);
                        thumbImage = image.GetThumbnailImage(100, 100, callb, new IntPtr());
                        thumbImage.Save(thumbImagePath);
                        thumbImage.Dispose();
                        e.Values["URL"] = virtualpath + "/" + fileName;
                        e.Values["ThumbURL"] = virtualpath + "/" + sypath + fileName;
                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('请上传jpg、gif、bmp或png格式的图片');</script>");
                        e.Cancel = true;
                    }
                }
            }
        }

        /// <summary>
        /// If the gridview has a command field where showdeletebutton is true, then 
        /// it add a confirm message. 
        /// This function should be called in the RowDataBound event
        /// </summary>
        public static void AddConfirmDelete(GridView gv, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (DataControlField dcf in gv.Columns)
                {
                    if (dcf.ToString() == "CommandField")
                    {
                        if (((CommandField)dcf).ShowDeleteButton == true)
                        {
                            e.Row.Cells[gv.Columns.IndexOf(dcf)].Attributes
                            .Add("onclick", "return confirm(\"确认要删除该条记录吗？\")");
                        }
                    }
                }
            }
        }

        protected void gvPicures_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddConfirmDelete((GridView)sender, e);
        }
    }
}